package com.example.user.service.Impl;

import com.example.core.exception.HotelException;
import com.example.model.check_in.PasswordDo;
import com.example.model.user.Role;
import com.example.model.user.User;
import com.example.model.user.UserRole;
import com.example.user.domain.LoginUser;
import com.example.user.mapper.UserMapper;
import com.example.user.mapper.UserRoleMapper;
import com.example.user.mapper.RoleMapper;
import com.example.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public int delete(Long id) {
        return userMapper.delete(id);
    }

    @Override
    public int insert(User user) {
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        return userMapper.insert(user);
    }

    @Override
    public User get(Long id) {
        return userMapper.get(id);
    }

    @Override
    public User getByUserName(String userName) {
        return userMapper.getByUserName(userName);
    }

    @Override
    public List<User> list(User user) {
        return userMapper.list(user);
    }

    @Override
    public int update(User record) {
        if (!record.getPassword().isEmpty()) {
            record.setPassword(passwordEncoder.encode(record.getPassword()));
        }
        return userMapper.update(record);
    }


    public int insertUserRole(User user) {
        Long[] roleIds = user.getRoleIds();
        if (roleIds.length > 0) {
            List<UserRole> list = new ArrayList<>();
            for (Long roleId : roleIds) {
                UserRole userRole = new UserRole();
                userRole.setUserId(user.getUserId());
                userRole.setRoleId(roleId);
                list.add(userRole);
            }
            return userRoleMapper.insertBatch(list);
        }
        return 0;
    }

    @Override
    public String getUserRoleGroup(String userName) {
        List<Role> list = roleMapper.listRoleByUserName(userName);
        if(!list.isEmpty()) {
            return list.stream().map(Role::getRoleKey).collect(Collectors.joining(","));
        } else {
            return null;
        }
    }

    @Override
    public String getUserRoleGroup(User user) {
        List<Role> list = user.getRoles();
        if(!list.isEmpty()) {
            return list.stream().map(Role::getRoleKey).collect(Collectors.joining(","));
        } else {
            return null;
        }
    }

    //重置单个密码
    @Override
    public int resetPassword(User user) {

//        user.setPassword("123456");
        user.setPassword(passwordEncoder.encode("123456"));
        return userMapper.resetPassword(user);
    }

    //重置全部密码
    @Override
    public int resetAllPasswords() {
        return userMapper.resetAllPasswords();
    }

    @Override
    public int updatePassword(PasswordDo passwordDo, LoginUser loginUser) {
        if (!passwordEncoder.matches(passwordDo.getOldPassword(),loginUser.getPassword())) {
            throw new HotelException(444, "原密码错误");
        }
        User user = loginUser.getUser();
        user.setPassword(passwordEncoder.encode(passwordDo.getNewPassword()));
        return userMapper.update(user);
    }

}
